package my.mark.mybaibaoxiang.twotiger.contract.utils;

import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.jfinal.log.Logger;
import com.twotiger.constant.PathCons;
import com.twotiger.util.PathUtil;

import javax.swing.*;
import java.awt.*;
import java.io.*;

/**
 * @author wxming.
 * @Description TODO.
 * @date 2020-12-4.
 */
public class ShuiYin {
    private static final Logger log = Logger.getLogger(ShuiYin.class);

    public static InputStream customerExport(InputStream temp) throws Exception {
        ByteArrayOutputStream tempArrayOutputStream = new ByteArrayOutputStream();
        try {

            PdfReader reader = new PdfReader(temp); //源文件
            //加水印的时候从原文件写到新的输出流里
            PdfStamper stamper = new PdfStamper(reader, tempArrayOutputStream);

            //下面这个不能用！！！下面这个是要加数字签名用的
            /*PdfStamper stamper = PdfStamper.createSignature(reader, tempArrayOutputStream,
                    '\0', null, true);*/
            //获取水印图片
            Image image=Image.getInstance(PathUtil.getRootClassPath("logo.png"));

            //获取最后一页
            int total = reader.getNumberOfPages();
            PdfContentByte content;
            // 循环对每页插入水印
            for (int i = 1; i < total + 1; i++) {
                // 水印的起始
                content = stamper.getUnderContent(i);
                content.beginText();
                // 开始写入水印
                for (int k = 0; k < 5; k++) {
                    for (int j = 0; j < 4; j++) {
                        image.setAbsolutePosition(150 * j, 170 * k);
                        content.addImage(image);
                    }
                }
                content.endText();
            }
            stamper.close();
            reader.close();

        } catch (Exception e) {
            log.error("水印失败", e);
        }
        return new ByteArrayInputStream(tempArrayOutputStream.toByteArray());
    }



    public static void waterMark(String inputFile,
                                 String outputFile, String waterMarkName) {
        try {
            PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));

            //加文字的相关内容
            /*BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   BaseFont.EMBEDDED);
            Rectangle pageRect = null;
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.3f);
            gs.setStrokeOpacity(0.4f);

            JLabel label = new JLabel();
            FontMetrics metrics;
            int textH = 0;
            int textW = 0;
            label.setText(waterMarkName);
            metrics = label.getFontMetrics(label.getFont());
            textH = metrics.getHeight();
            textW = metrics.stringWidth(label.getText());*/

            Image image=Image.getInstance(PathUtil.getRootClassPath("logo.png"));
            PdfContentByte under;
            int total = reader.getNumberOfPages() + 1;
            for (int i = 1; i < total; i++) {
                under = stamper.getOverContent(i);// 在内容上方加水印
                //content = stamper.getUnderContent(i);//在内容下方加水印
                /*gs.setFillOpacity(0.2f);*/
                // content.setGState(gs);
                under.beginText();

                for (int k = 0; k < 5; k++) {
                    for (int j = 0; j < 4; j++) {
                        image.setAbsolutePosition(150 * j, 170 * k);
                        under.addImage(image);
                    }
                }

                /*under.setFontAndSize(base, 50);
                under.setTextMatrix(70, 200);
                under.showTextAligned(Element.ALIGN_CENTER, "测试水印！", 300,350, 55);
*/
                under.endText();
            }

            //一定不要忘记关闭流
            stamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        waterMark("111.pdf","111222.pdf","牧野之印");
        System.out.println("");
    }

}
